package cn.cuilan.common.pipeline;

import cn.cuilan.common.entity.ProtocolMessage;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;

/**
 * 异常解析器
 *
 * @author zhang.yan
 */
@ChannelHandler.Sharable
@Slf4j
public class ParseExceptionHandler extends ChannelHandlerAdapter {

    private final boolean includeErrorDetails;

    public ParseExceptionHandler(boolean includeErrorDetails) {
        this.includeErrorDetails = includeErrorDetails;
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        if (cause instanceof ParseException) {
            final ProtocolMessage message = createErrorResponseMessage((ParseException) cause);
            ctx.writeAndFlush(message);
        }
        super.exceptionCaught(ctx, cause);
    }

    private ProtocolMessage createErrorResponseMessage(ParseException cause) {
        log.info("Error Response Message 未实现");
        return null;
    }
}
